<template>
    <!-- 企业 -->
    <div class="company">
        <div style="height:2px;background-color: #F5F6F7;"></div>
        <div class="headTabs padding15All ffffff">
            <TabsMain type="company" @Listsearch="Listsearch"/>
        </div>
        <div style="height:15px;background-color: #F5F6F7;"></div>
        <div class="contentTable ffffff ">
            <div class="padding15All tableMain">
                <TableTop type="company" :multipleSelection="multipleSelection">
                    <template v-slot:company>
                        <el-button @click="dialogFormVisible = true" size="small" type="primary">添加企业</el-button>
                    </template>
                    <template v-slot:delete>
                        <el-button @click="delevent(1)" size="small" type="info">删除</el-button>
                    </template>
                </TableTop>
                <el-table ref="multipleTable" :data="list" tooltip-effect="dark" style="width: 100%;height:100%;"
                 @selection-change="handleSelectionChange" :empty-text='emtypetxt'>
                    <el-table-column type="selection" width="55">
                    </el-table-column>
                    <el-table-column fixed prop="contactsEnterpriseName" label="企业名称" width="233">
                    </el-table-column>
                    <el-table-column prop="legalPerson" label="法人" width="130">
                    </el-table-column>
                    <el-table-column prop="creditCode" label="统一社会信用代码" width="230">
                    </el-table-column>
                    <el-table-column label="认证状态" width="150">
                        <template slot-scope="scope">
                            <el-tag size="small" v-if="scope.row.authType == 1" type="success">已认证</el-tag>
                            <el-tag size="small" v-if="scope.row.authType == 0" type="warning">未认证</el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column prop="insertTime" label="创建时间" width="300">
                    </el-table-column>
                    <el-table-column prop="staffNum" label="员工数量" width="">
                    </el-table-column>
                    <el-table-column fixed="right" label="操作" width="320">
                        <template slot-scope="scope">
                            <el-button @click="handleClick(scope.row)" type="text" size="small">详情</el-button>
                            <!-- <el-button @click="dialogFormVisible = true" type="text" size="small">编辑</el-button>
                            <el-button type="text" size="small">工商信息</el-button> -->
                            <el-button @click="delevent(2, scope.row)" type="text" size="small">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div class="flexleft padding15" style="justify-content: flex-end;">
                <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                    :current-page="params.pageNo" :page-sizes="[10, 20, 50, 100]" :page-size="params.pageSize"
                    layout="total, prev, pager, next, sizes, jumper" :total="total">
                </el-pagination>
            </div>
        </div>
        <el-dialog title="添加企业" :visible.sync="dialogFormVisible" width="560px">
            <el-form :model="form">
                <el-form-item required label="企业名称" :label-width="formLabelWidth">
                    <el-input v-model="form.name" placeholder="请输入企业名称" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="法人" :label-width="formLabelWidth">
                    <el-input v-model="form.name" placeholder="请输入法人" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item required label="统一社会信用代码" :label-width="formLabelWidth">
                    <el-input v-model="form.name" placeholder="请输入统一社会信用代码" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="备注" :label-width="formLabelWidth">
                    <el-input maxlength="200" type="textarea" v-model="form.name" placeholder="请输入备注" autocomplete="off"
                        show-word-limit></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取消</el-button>
                <el-button type="primary" @click="dialogFormVisible = false">保存</el-button>
                <el-button type="primary" @click="dialogFormVisible = false">保存并继续添加</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import TabsMain from '../components/TabsMain.vue'
import TableTop from '../components/TableTop.vue'
import { oppositeList, deleteBatch } from '@/api/opposite'
import Cookies from 'js-cookie'//获取cookies的信息
export default {
    name: 'Company',
    components: {
        TableTop,
        TabsMain,
    },

    data() {
        return {
            dialogFormVisible: false,
            formLabelWidth: '180px',
            form: {
                name: '',
                region: '',
                date1: '',
                date2: '',
            },
            // 相对方企业请求参数
            params: {
                contactsEnterpriseName: "",//联系人公司名称
                contactsName: "",//联系人名称
                contactsPhone: "",//联系人电话
                contractId: "",//合同id
                enterpriseId: "",//企业id
                hasEnterprise: "",//是否有企业
                id: "",//常用联系人id
                insertTime: "",//申请时间
                isAuth: "",//是否认证 0 否 1是
                isHide: "",//删除标识 0-否 1-是
                keywordsTxt: "",//关键字搜索key
                type: 1,//联系人类型 0-个人 1-企业
                userId: Cookies.get('admin_id'),//用户id//获取cookies中用户id的信息
                pageNo: 1,
                pageSize: 10
            },
            emtypetxt: '',
            list: [],
            total: 0,
            multipleSelection: [],

        };
    },
    mounted() {
        this.getList()
    },
    methods: {
        // 筛选
        Listsearch(e){
            this.params.contactsEnterpriseName = e.contactsEnterpriseName;//企业名称
            this.params.contactsName = e.contactsName;//姓名
            this.params.contactsPhone = e.contactsPhone;//手机号
            this.params.isAuth = e.isAuth;//认证状态
            this.getList()
        },
        getList() {
            this.emtypetxt = '加载中...';
            oppositeList(this.params).then(res => {
                this.list = res.result.records
                this.total = res.result.total
                if (this.list.length == 0) {
                    this.emtypetxt = '暂无数据'
                }
            }).catch(err => {
                console.log("请求失败", err)
            })
        },
        // 跳转企业详情
        handleClick(row) {
            this.$router.push({ name: 'companyDetail',query:{id:row.contactsId } },()=>{})
        },
         // 删除单个
        delevent(value, row) {
            this.$confirm('确定删除当前数据？', '删除', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                if (value == 1) {
                    this.delAll()
                } else {
                    this.delitem(row)
                }
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        delAll() {
            let ids = ""
            let arr = []
            console.log(this.multipleSelection, "jinglei")
            this.multipleSelection.forEach(item => {
                arr.push(item.contactsId)
            })
            ids = arr.join()
            deleteBatch(ids).then(res => {
                this.$message({
                    type: 'success',
                    message: '删除成功!'
                });
            }).catch(err => {
                this.$message.error(err.message);
            })
        },
        delitem(row) {
            let ids = row.contactsId
            deleteBatch(ids).then(res => {
                this.$message({
                    type: 'success',
                    message: '删除成功!'
                });
            }).catch(err => {
                this.$message.error(err.message);
            })
        },
        handleSizeChange(val) {
            this.params.pagesize = val
            this.getList()
        },
        handleCurrentChange(val) {
            this.params.pageNo = val
            this.getList()
        },
        handleSelectionChange(val) {
            this.multipleSelection = val;
        }
    }

}
</script>

<style lang="scss"></style>

<style lang="scss" scoped>
.company {
    height: 100%;
    padding-top: 1px;
    box-sizing: border-box;
    background-color: #fff;
}
</style>